Page({
    data: {
        user: {}
        , serverBirth: ''
    }
    , onLoad: function () {
        var zz_that = this;
        getApp().page.onLoad(zz_that, {});

        getApp().core.showLoading({
            title: '正在加载'
            , mask: true
        });

        getApp().request({
            url: getApp().api.user.index
            , success: function (result) {
                getApp().core.hideLoading();
                if (result.code == 0) {
                    zz_that.setData({
                        user: result.data.user_info
                        , serverBirth: result.data.user_info.birthday
                    });
                }
            }
        });
    }
    , onChooseAvatar: function (e) {
        var zz_that = this;
        getApp().core.uploadFile({
            url: getApp().api.default.upload_image
            , filePath: e.detail.avatarUrl
            , name: 'image'
            , success (result) {
                result = JSON.parse(result.data);
                if (result.code == 0) {
                    var user = zz_that.data.user;
                    user.avatar_url = result.data.url;
                    zz_that.setData({user});
                }
            }
        });
    }
    , onInputNickname: function (e) {
        console.log(e)
    }
    , zz_selectBirth(e) {
        var zz_that = this;
        var user = zz_that.data.user;
        user.birthday = e.detail.value;
        zz_that.setData({user});
    }
    , save: function (e) {
        var zz_that = this;
        if (!zz_that.data.user.avatar_url) {
            wx.showToast({
                title: '请上传头像'
                , icon: 'none' 
            });
            return false;
        }
        
        if (!e.detail.value.nickname) {
            wx.showToast({
                title: '请输入昵称'
                , icon: 'none' 
            });
            return false;
        }

        getApp().core.showLoading({
            title: '正在保存'
            , mask: true
        });

        getApp().request({
            url: getApp().api.user.zz_update_nickavar
            , method: 'post'
            , data: {
                avatar_url: zz_that.data.user.avatar_url
                , nickname: e.detail.value.nickname
                , birthday: zz_that.data.user.birthday
            }
            , success: function (result) {
                getApp().core.hideLoading();
                if (result.code == 0) {
                    getApp().core.showModal({
                        title: '提示'
                        , content: result.msg
                        , showCancel: false
                        , success: function (result2) {
                            if (result2.confirm) {
                                getApp().core.navigateBack();
                            }
                        }
                    });
                } else {
                    wx.showToast({
                        title: result.msg
                        , icon: 'none' 
                    });
                }
            }
        });
    }
});
